package main

import (
	"math/rand"
	"strings"
)

type Player struct {
	Chips  int
	In     int
	Record int
	Sum    int
	Cards  []string
}

func (p *Player) PrintCards() string {
	b := new(strings.Builder)
	for i, c := range p.Cards {
		b.WriteString(c)
		if i < len(p.Cards)-1 {
			b.WriteString(" ")
		}
	}
	return b.String()
}

func (p *Player) CalcSum() {
	p.Sum = 0
	ace := false
	for _, card := range p.Cards {
		switch card[len(card)-1] {
		case 'A':
			p.Sum += 11
			ace = true
			if p.Sum > 21 {
				p.Sum -= 10
				ace = false
			}
		case '0', 'J', 'Q', 'K':
			p.Sum += 10
		default:
			p.Sum += int(card[len(card)-1] - '0')
		}
		if p.Sum > 21 && ace {
			p.Sum -= 10
			ace = false
		}
	}
}

type CardSet struct {
	Cards []string
	Idx   int
}

func (c *CardSet) Init() {
	c.Cards = make([]string, 0)
	for i := 0; i < 4; i++ {
		suit := CardSuits[i]
		for i := 0; i < 13; i++ {
			c.Cards = append(c.Cards, suit+" "+CardNums[i])
		}
	}
}

func (c *CardSet) Shuffle() {
	rand.Shuffle(len(c.Cards), func(i, j int) { c.Cards[i], c.Cards[j] = c.Cards[j], c.Cards[i] })
	c.Idx = 0
}

func (c *CardSet) Deal() string {
	card := c.Cards[c.Idx]
	c.Idx++
	return card
}
